#!/bin/bash

echo "=== 维权力项目启动脚本 ==="

# 检查Java环境
if ! command -v java &> /dev/null; then
    echo "❌ 未找到Java环境，请先安装JDK 8+"
    exit 1
fi

# 检查Maven环境
if ! command -v mvn &> /dev/null; then
    echo "❌ 未找到Maven环境，请先安装Maven 3.6+"
    exit 1
fi

# 检查Node.js环境
if ! command -v node &> /dev/null; then
    echo "❌ 未找到Node.js环境，请先安装Node.js 16+"
    exit 1
fi

echo "✅ 环境检查通过"

# 启动后端服务
echo "🚀 启动后端服务..."
cd weiquan-backend

# 清理并编译
echo "📦 编译后端项目..."
mvn clean compile -DskipTests

if [ $? -eq 0 ]; then
    echo "✅ 后端编译成功"
    
    # 启动Spring Boot应用
    echo "🔥 启动Spring Boot应用..."
    mvn spring-boot:run &
    BACKEND_PID=$!
    
    echo "后端服务已启动，PID: $BACKEND_PID"
    echo "后端服务地址: http://localhost:8080/api"
else
    echo "❌ 后端编译失败"
    exit 1
fi

# 返回根目录
cd ..

# 启动前端服务
echo "🚀 启动前端服务..."
cd weiquan-frontend

# 安装依赖
if [ ! -d "node_modules" ]; then
    echo "📦 安装前端依赖..."
    npm install
fi

# 启动开发服务器
echo "🔥 启动前端开发服务器..."
npm run dev &
FRONTEND_PID=$!

echo "前端服务已启动，PID: $FRONTEND_PID"
echo "前端服务地址: http://localhost:3000"

echo ""
echo "=== 服务启动完成 ==="
echo "后端服务: http://localhost:8080/api"
echo "前端服务: http://localhost:3000"
echo "数据库监控: http://localhost:8080/api/druid (admin/admin123)"
echo ""
echo "按 Ctrl+C 停止所有服务"

# 等待用户中断
wait